package main

import (
	"fmt"
	"github.com/tidwall/gjson"
)

// 牛逼的 json 跨层级解析
func main() {
	const json = `{
  "name": {"first": "Tom", "last": "Anderson"},
  "age":37,
  "children": ["Sara","Alex","Jack"],
  "fav.movie": "Deer Hunter",
  "friends": [
    {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
    {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
    {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
  ]
}`
	fmt.Println(gjson.Get(json, "name.first"))                       // Tom
	fmt.Println(gjson.Get(json, "age"))                              // 37
	fmt.Println(gjson.Get(json, "children"))                         // ["Sara","Alex","Jack"]
	fmt.Println(gjson.Get(json, "children.#"))                       // 3
	fmt.Println(gjson.Get(json, "children.1"))                       // Alex
	fmt.Println(gjson.Get(json, "child*.2"))                         // Jack
	fmt.Println(gjson.Get(json, "c?ildren.0"))                       //Sara
	fmt.Println(gjson.Get(json, "friends.#.first"))                  //["Dale","Roger","Jane"]
	fmt.Println(gjson.Get(json, "friends.1.first"))                  // Craig
	fmt.Println(gjson.Get(json, "friends.#(last==Murphy).first"))    // Dale
	fmt.Println(gjson.Get(json, "friends.#(last==Murphy)#.first"))   // ["Dale","Jane"]
	fmt.Println(gjson.Get(json, "friends.#(age>45)#.last"))          // ["Craig","Murphy"]
	fmt.Println(gjson.Get(json, `friends.#(first%"D*").last`))       // Murphy
	fmt.Println(gjson.Get(json, `friends.#(first!%"D*").last`))      // Craig
	fmt.Println(gjson.Get(json, `friends.#(nets.#(=="fb"))#.first`)) // ["Dale","Roger"]

}
